package com.jing.my;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import dev.utils.app.AppUtils;
import dev.utils.app.DeviceUtils;
import dev.utils.app.SnackbarUtils;
import dev.utils.app.toast.ToastUtils;

public class SecondActivity extends AppCompatActivity {
	BottomNavigationView bv;
	CoordinatorLayout cl;
boolean back=false;
ActionBar a;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.second);
		bv = findViewById(R.id.bottom_navigation);
		cl = findViewById(R.id.coor);
		a=getSupportActionBar();
		bv.setSelectedItemId(R.id.page_2);
		change(new fragtwo());
		
		bv.setOnItemSelectedListener(v -> {
			switch (v.getItemId()) {
			case R.id.page_1:
			//	BadgeDrawable b = bv.getOrCreateBadge(v.getItemId());
			//	b.setVisible(true);
			//	b.setNumber(999);
			change(new fragone());
		//	new Handler(Looper.getMainLooper()).post(v->{});
				ToastUtils.showShort("第一页");
				home();
				break;
			case R.id.page_2:
			change(new fragtwo());
				ToastUtils.showShort("第二页");
				home();
				break;
				case R.id.page_3:
				change(new fragthree());
				ToastUtils.showShort("第三页");
				home();
				break ;
			
		//		bv.setSelectedItemId(v.getItemId());
				
			}

			return true;
		});
	}

	void change(Fragment f) {
		getSupportFragmentManager().beginTransaction().replace(R.id.fragment, f).commit();

	}
@Override
public void onBackPressed() {
	if(back==false)
			{
			super.onBackPressed();	
				}
				else {
					back=false;
					change(new fragtwo());
					bar(false);
					
					}
	
}
	@Override
	public boolean onCreateOptionsMenu(Menu arg0) {
		getMenuInflater().inflate(R.menu.main_menu, arg0);
		return super.onCreateOptionsMenu(arg0);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem arg0) {
		switch (arg0.getItemId()) {
			case android.R.id.home:
			back=false;
			bar(back);
			change(new fragtwo());
			
			
			return true;
		case R.id.setting:
		back=true;
		//	getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new MySettingsFragment()).commit();
		change(new MySettingsFragment());
		bar(back);
			
			return true;

		default:
			return super.onOptionsItemSelected(arg0);

		}
	}
	void bar(boolean b)
			{
		//	ActionBar a=getSupportActionBar();
			a.setDisplayHomeAsUpEnabled(b);
			a.setDisplayShowHomeEnabled(b);	
				
				}
				void home()
						{
			back=false;
			bar(back);
			
							
							
							}

}